
package hackathon.techbooster.tanabata;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

public class PreferenceUtils {
// @formatter:off
    public static final String CreateUrl = "";
    public static final String ListUrl = "";

    private static final String PREFERENCE = "pref";
    private static final String PREF_KEY_CREATE_ID = "create_id";
    private static final String PREF_KEY_NAME = "name";
    private static final String PREF_KEY_HOPE = "hope";
    private static final String PREF_KEY_TWITTER = "twitter";
    private static final String PREF_KEY_JSON = "json";

    public static final String KEY_ID = "id";
    public static final String KEY_BACK_RES = "resource_id";
    public static final String KEY_NAME = "name";
    public static final String KEY_HOPE = "hope";
    public static final String KEY_TWITTER = "twitter";
    public static final String KEY_TWEET_MESSAGE = "message";

    public static final String JSON_ID = "id";
    public static final String JSON_NAME = "name";
    public static final String JSON_HOPE = "hope";

    public static final String FONT_NAME = "ipam.ttf";
// @formatter:on

    public static String getCreatedId(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREFERENCE, Context.MODE_PRIVATE);

        return prefs.getString(PREF_KEY_CREATE_ID, "-1");
    }

    public static String[] getData(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREFERENCE, Context.MODE_PRIVATE);
        String[] data = new String[3];
        data[0] = prefs.getString(PREF_KEY_NAME, "");
        data[1] = prefs.getString(PREF_KEY_HOPE, "");
        data[2] = String.valueOf(prefs.getBoolean(PREF_KEY_TWITTER, true));
        return data;
    }

    public static void setCreatedId(Context context, String createId, String name, String hope,
            boolean isTwitter) {
        SharedPreferences prefs = context.getSharedPreferences(PREFERENCE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(PREF_KEY_CREATE_ID, createId.trim());
        editor.putString(PREF_KEY_NAME, name.trim());
        editor.putString(PREF_KEY_HOPE, hope.trim());
        editor.putBoolean(PREF_KEY_TWITTER, isTwitter);
        editor.commit();
    }

    public static String getJson(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREFERENCE, Context.MODE_PRIVATE);

        return prefs.getString(PREF_KEY_JSON, "");
    }

    public static void setJson(Context context, String json) {
        SharedPreferences prefs = context.getSharedPreferences(PREFERENCE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(PREF_KEY_JSON, json.trim());
        editor.commit();
    }

    public static boolean checkFont(Context context) {
        return getFontFile(context).exists();
    }

    public static File getFontFile(Context context) {
        File baseDirectory = new File(Environment.getExternalStorageDirectory(),
                context.getPackageName());
        if (!baseDirectory.exists()) {
            baseDirectory.mkdirs();
        }
        File font = new File(baseDirectory, FONT_NAME);

        return font;
    }

    public static String encodeUrl(final String words) throws UnsupportedEncodingException {
        return URLEncoder.encode(words, "UTF-8");
    }

}
